package com.mall.common.service.impl;

import net.sf.json.JSONArray;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.mall.b2c.util.SettingUtils;
import com.mall.common.bean.Pager;
import com.mall.common.exception.NoticeException;
import com.mall.common.service.BackupService;

/**
 * 
 * @author wangzx
 */
@Service
@Transactional
public class BackupServiceImpl implements BackupService {
	
	@Override
	public void backupAlbum() {
		String albumBackupUrl = SettingUtils.get().getAlbumBackupUrl();
		if(StringUtils.isBlank(albumBackupUrl)) {
			throw new NoticeException("相册备份Url为空");
		}
		HttpClient httpClient = new HttpClient();
		PostMethod postMethod = new PostMethod(albumBackupUrl);
		postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8");
		String result = "";
		try {
			httpClient.executeMethod(postMethod);
			result = new String(postMethod.getResponseBody(), "utf-8");
		} catch (Exception e) {
			e.printStackTrace();
			throw new NoticeException("相册备份出错");
		}
		if(StringUtils.isBlank(result)) {
			throw new NoticeException("相册备份不成功");
		}
	}

	@Override
	public Pager getAlbums(Pager pager) {
		String albumBackupUrl = SettingUtils.get().getAlbumBackupUrl();
		if(StringUtils.isBlank(albumBackupUrl)) {
			throw new NoticeException("相册备份Url为空");
		}
		HttpClient httpClient = new HttpClient();
		GetMethod getMethod = new GetMethod(albumBackupUrl);
		getMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8");
		String result = "";
		try {
			httpClient.executeMethod(getMethod);
			result = new String(getMethod.getResponseBody(), "utf-8");
		} catch (Exception e) {
			e.printStackTrace();
			throw new NoticeException("后去相册备份异常");
		}
		
		if(StringUtils.isNotBlank(result)) {
			JSONArray array = JSONArray.fromObject(result);
			pager.setList(array);
		}
		return pager;
	}

	@Override
	public void restoreAlbum(String path) {
		String albumRestoreUrl = SettingUtils.get().getAlbumRestoreUrl();
		if(StringUtils.isBlank(albumRestoreUrl)) {
			throw new NoticeException("相册还原Url为空");
		}
		HttpClient httpClient = new HttpClient();
		PostMethod postMethod = new PostMethod(albumRestoreUrl + path);
		postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8");
		Integer responseCode = null;
		try {
			responseCode = httpClient.executeMethod(postMethod);
		} catch (Exception e) {
			e.printStackTrace();
			throw new NoticeException("相册还原出错");
		}
		if(responseCode != 200) {
			throw new NoticeException("相册还原不成功");
		}
	}

	@Override
	public void backupDb() {
		String dbBackupUrl = SettingUtils.get().getDbBackupUrl();
		if(StringUtils.isBlank(dbBackupUrl)) {
			throw new NoticeException("数据库备份Url为空");
		}
		HttpClient httpClient = new HttpClient();
		PostMethod postMethod = new PostMethod(dbBackupUrl);
		postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8");
		String result = "";
		try {
			httpClient.executeMethod(postMethod);
			result = new String(postMethod.getResponseBody(), "utf-8");
		} catch (Exception e) {
			e.printStackTrace();
			throw new NoticeException("数据库备份出错");
		}
		if(StringUtils.isBlank(result)) {
			throw new NoticeException("数据库备份不成功");
		}
	}

	@Override
	public Pager getDbs(Pager pager) {
		String dbBackupUrl = SettingUtils.get().getDbBackupUrl();
		if(StringUtils.isBlank(dbBackupUrl)) {
			throw new NoticeException("数据库备份Url为空");
		}
		HttpClient httpClient = new HttpClient();
		GetMethod getMethod = new GetMethod(dbBackupUrl);
		getMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8");
		String result = "";
		try {
			httpClient.executeMethod(getMethod);
			result = new String(getMethod.getResponseBody(), "utf-8");
		} catch (Exception e) {
			e.printStackTrace();
			throw new NoticeException("获取数据库备份异常");
		}
		
		if(StringUtils.isNotBlank(result)) {
			JSONArray array = JSONArray.fromObject(result);
			pager.setList(array);
		}
		return pager;
	}

	@Override
	public void restoreDb(String path) {
		String dbRestoreUrl = SettingUtils.get().getDbRestoreUrl();
		if(StringUtils.isBlank(dbRestoreUrl)) {
			throw new NoticeException("数据库还原Url为空");
		}
		HttpClient httpClient = new HttpClient();
		PostMethod postMethod = new PostMethod(dbRestoreUrl + path);
		postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8");
		Integer responseCode = null;
		try {
			responseCode = httpClient.executeMethod(postMethod);
		} catch (Exception e) {
			e.printStackTrace();
			throw new NoticeException("数据库还原出错");
		}
		if(responseCode != 200) {
			throw new NoticeException("数据库还原不成功");
		}
	}
}